package com.situ.company.employee.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.situ.company.employee.model.EmployeeModel;
import com.situ.company.util.JDBCUtil;

public class EmployeeDao {
	private static String table = "st_company.employee";
	private static String cols = "code,name,pass,depCode,image";
	private Connection conn = null;
	private PreparedStatement ps = null;

	public Integer insert(EmployeeModel model) {
		StringBuffer sql = new StringBuffer("insert into ");
		sql.append(table).append("(").append(cols).append(")values(?,?,?,?,?)");
		try {
			conn = JDBCUtil.getConnection();
			ps = conn.prepareStatement(sql.toString());
			ps.setString(1, model.getCode());
			ps.setString(2, model.getName());
			ps.setString(3, model.getPass());
			ps.setString(4, model.getDepCode());
			ps.setString(5, model.getImage());
			return ps.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			JDBCUtil.close(conn, ps);
		}
		return null;
	}

	public Integer delete(EmployeeModel model) {
		StringBuffer sql = new StringBuffer("delete from ");
		sql.append(table);// where code = ?
		List<Object> vals = appendWhere(sql, model);
		try {
			conn = JDBCUtil.getConnection();
			ps = conn.prepareStatement(sql.toString());
			for (int i = 0; i < vals.size(); i++) {
				ps.setObject(i + 1, vals.get(i));
			}
			return ps.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			JDBCUtil.close(conn, ps);
		}
		return null;
	}

	public List<EmployeeModel> selectList(EmployeeModel model) {
		List<EmployeeModel> list = new ArrayList<EmployeeModel>();
		StringBuffer sql = new StringBuffer("select ");
		sql.append(cols);
		sql.append (",(select d.name from st_company.department d where d.code=depCode) depName") ;
		sql.append(" from ").append(table);
		List<Object> vals = appendWhere(sql, model);
		ResultSet rs = null;
		try {
			conn = JDBCUtil.getConnection();
			ps = conn.prepareStatement(sql.toString());
			if (vals.size() == 0) {
				rs = ps.executeQuery();
			} else {
				for (int i = 0; i < vals.size(); i++) {
					ps.setObject(i + 1, vals.get(i));		
				}
				rs = ps.executeQuery();
			}
			while (rs.next()) {
				EmployeeModel mod = new EmployeeModel();
				mod.setCode(rs.getString("code"));
				mod.setName(rs.getString("name"));
				mod.setPass(rs.getString("pass"));
				mod.setDepCode(rs.getString("depCode"));
				mod.setImage(rs.getString("image"));
				mod.setDepName(rs.getString("depName"));
				list.add(mod);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			JDBCUtil.close(conn, ps, rs);
		}
		return list;
	}

	private List<Object> appendWhere(StringBuffer sql, EmployeeModel model) {
		sql.append(" where 1=1");
		List<Object> objs = new ArrayList<Object>();
		String code = model.getCode();
		if (code != null && !code.trim().isEmpty()) {// !"".equals(code.trim())
			sql.append(" and code like ?");
			objs.add(code);
		}
		String name = model.getName();
		if (name != null && !name.trim().isEmpty()) {
			sql.append(" and name like ?");
			objs.add(name);
		}
		String order = model.getOrderby();
		if (order != null && !order.trim().isEmpty()) {
			sql.append(" order by ? ").append(order);
		}
		if (model.isPageOn()) {
			sql.append(" limit ?,? ");
			objs.add(model.getRowStart());
			objs.add(model.getRowCount());
		}
		return objs;
	}

	public Integer update(EmployeeModel model) {
		StringBuffer sql = new StringBuffer("update ");
		sql.append(table);// set name=?,tel=? where code=?
		List<Object> vals = appendSet(sql, model);
		System.out.println(sql);
		try {
			conn = JDBCUtil.getConnection();
			ps = conn.prepareStatement(sql.toString());
			for (int i = 0; i < vals.size(); i++) {
				ps.setObject(i + 1, vals.get(i));
			}
			System.out.println(ps.executeUpdate());
			return ps.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			JDBCUtil.close(conn, ps);
		}
		return null;
	}

	private List<Object> appendSet(StringBuffer sql, EmployeeModel model) {
		sql.append(" set id = id ");
		List<Object> objs = new ArrayList<Object>();

		String name = model.getName();
		if (name != null) {
			sql.append(",name = ? ");
			objs.add(name);
		}
		String depCode = model.getDepCode();
		if (depCode != null) {
			sql.append(",depCode = ? ");
			objs.add(depCode);
		}
		String pass = model.getPass();
		if (pass != null) {
			sql.append(",pass = ? ");
			objs.add(pass);
		}
		String image = model.getImage();
		if (image != null) {
			sql.append(",image = ? ");
			objs.add(image);
		}
		String code = model.getCode();
		sql.append("where code = ? ");
		objs.add(code);
		return objs;
	}
	
	public int selectCount(EmployeeModel model) {

		StringBuffer sql = new StringBuffer("select count(id) from ");
		sql.append(table);
		List<Object> vals = appendWhere(sql, model);

		System.out.println(sql);

		ResultSet rs = null;
		try {
			conn = JDBCUtil.getConnection();
			ps = conn.prepareStatement(sql.toString());
			for (int i = 0; i < vals.size(); i++) {
				ps.setObject(i + 1, vals.get(i));	
			}
			rs = ps.executeQuery();
			if (rs.next()) {
				return rs.getInt(1);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			JDBCUtil.close(conn, ps, rs);
		}
		return 0;
	}
}
